package com.imooc.girl.handler;

import com.imooc.girl.exception.GirlException;
import com.imooc.girl.result.Result;
import com.imooc.girl.result.ResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description: 统一异常处理
 * @program: girl
 * @author: Administrator
 * @date: 2019/2/19 16:02
 */
@ControllerAdvice
public class MyExceptionHandler {
	
	private static final Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);
	
	/**
	 * 处理Exception异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result<?> handlerException(Exception e) {
		if (e instanceof GirlException) {
			GirlException girlException = (GirlException) e;
			return ResultHandler.error(girlException.getErrorCode(), girlException.getMessage());
		} else {
			logger.error("【系统异常】 = {}", e);
			return ResultHandler.error("-1", "未知错误");
		}

	}

}
